package com.example.edianzu.somur.module.health.adapter;

import android.graphics.Color;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.edianzu.somur.R;
import com.example.edianzu.somur.api.APIConstants;
import com.example.edianzu.somur.api.manager.APIManager;
import com.example.edianzu.somur.module.health.bean.HealthPlan;
import com.example.edianzu.somur.utils.content.L;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.badgeview.BGABadgeLinearLayout;
import cn.bingoogolapple.badgeview.BGABadgeViewHelper;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import me.leolin.shortcutbadger.ShortcutBadger;

/**
 * author：风不会停息 on 2017/7/26 16:14
 * mailbox：yh131412hys@163.com
 * project：Somur
 * tel：13295355900
 * describe：
 */

public class HealthAdapter extends RecyclerView.Adapter<HealthAdapter.ViewHolder> {

    private List<HealthPlan.DataBean.PushListBean> pushListBeen;
    private OnItemClickListener onItemClickListener;

    public HealthAdapter(List<HealthPlan.DataBean.PushListBean> pushListBeen) {
        this.pushListBeen = pushListBeen;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_health, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        final HealthPlan.DataBean.PushListBean pushListBean = pushListBeen.get(position);
        holder.fragmentHealthContent1.setText(pushListBean.getTitle());
        holder.fragmentHealthRemark1.setText(pushListBean.getRemark());
        holder.fragmentHealthName1.setText("健康管理师 : "+ pushListBean.getName());
//    is_look 0代表没查看过 1代表查看
        if (pushListBean.getIs_look()==0) {

            initBadgeView(holder);
        }

        if (onItemClickListener != null) {

            holder.fragmentHealthLibearLayout1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {

                    onItemClickListener.onItemClickListener(v,holder.getLayoutPosition());
//                    更改方案状态
                    APIManager.getApiManagerInstance().isCheck(new Observer<HealthPlan>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NonNull HealthPlan healthPlan) {

                            if (healthPlan.getStatus()== APIConstants.STATUS) {

                                L.d("更改状态为已查看");
                                //       移除图标上的消息
                                ShortcutBadger.removeCount(v.getContext());
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    },pushListBean.getId());
                }
            });
        }
    }

    private void initBadgeView(ViewHolder holder) {
        holder.fragmentHealthLibearLayout1.showCirclePointBadge();
        //初始化
//        holder.fragmentHealthLibearLayout1.showTextBadge("9");
//        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeTextSizeSp(15);
        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeTextColorInt(Color.WHITE);
        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeBgColorInt(Color.RED);
        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setDragable(true);
//            holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgePaddingDp(6);
//        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeVerticalMarginDp(2);
//        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeHorizontalMarginDp(2);
        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeGravity(BGABadgeViewHelper.BadgeGravity.RightTop);
//        holder.fragmentHealthLibearLayout1.getBadgeViewHelper().setBadgeBorderWidthDp(2);

        /*属性名	说明	默认值
        badge_bgColor	徽章背景色	Color.RED
        badge_textColor	徽章文本的颜色	Color.WHITE
        badge_textSize	徽章文本字体大小	10sp
        badge_verticalMargin	徽章背景与宿主控件上下边缘间距离	4dp
        badge_horizontalMargin	徽章背景与宿主控件左右边缘间距离	4dp
        badge_padding	徽章文本边缘与徽章背景边缘间的距离	4dp
        badge_gravity	徽章在宿主控件中的位置	BGABadgeImageView和BGABadgeRadioButton是右上方，其他控件是右边垂直居中
        badge_dragable	是否开启拖拽删除徽章	false
        badge_isResumeTravel	拖拽徽章超出轨迹范围后，再次放回到轨迹范围时，是否恢复轨迹	false
        badge_borderWidth	徽章描边宽度	0dp
        badge_borderColor	徽章描边颜色	Color.WHITE
        badge_dragExtra	触发开始拖拽徽章事件的扩展触摸距离	4dp*/
    }

    @Override
    public int getItemCount() {
        return pushListBeen != null ? pushListBeen.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.fragment_health_LibearLayout_1)
        BGABadgeLinearLayout fragmentHealthLibearLayout1;
        @BindView(R.id.fragment_health_content_1)
        AppCompatTextView fragmentHealthContent1;
        @BindView(R.id.fragment_health_remark_1)
        AppCompatTextView fragmentHealthRemark1;
        @BindView(R.id.fragment_health_name_1)
        AppCompatTextView fragmentHealthName1;

        ViewHolder(View view) {

            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void removeData(){

        if (pushListBeen != null) {

            pushListBeen.clear();
            notifyDataSetChanged();
        }
    }

    public void addData(List<HealthPlan.DataBean.PushListBean> list){

        if (pushListBeen != null) {

            pushListBeen.addAll(list);
            notifyDataSetChanged();
        }
    }

    public interface OnItemClickListener {

        void onItemClickListener(View view, int position);
    }

    public interface OnItemLongClickListener {

        void onItemLongClickListener(View view, int position);
    }
}
